import React from 'react';
import { Route } from 'react-router-dom';
import { RouteProps } from 'react-router-dom';

export interface IKeepAliveProps extends RouteProps {
  component: any;
}

export function KeepAlive(props: IKeepAliveProps) {
  const { path, component: Component, ...restProps } = props;

  return (
    <Route
      {...restProps}
      render={(_props) => {
        const isMatch = _props.location.pathname.startsWith(path as string);
        return (
          <div style={{ display: isMatch ? 'block' : 'none' }}>
            <Component {..._props} />
          </div>
        );
      }}
    />
  );
}
